document
    .write("<script language=javascript src='/js/areaJson.js'></script>");

function provSelect() {
    var data = "";
    $("#provinceCode").append("<option value=''>--请选择--</option>");

    $.each(areaJson, function (i, prov) {
        $("#provinceCode").append("<option value='" + prov.value + "'>" + prov.text + "</option>");
    });
}

// 赋值市级函数
function citySelect(prov_id) {
    if (prov_id < 0 || typeof (areaJson[prov_id].children) == "undefined") {
        return;
    }
    $("#cityCode").append("<option value=''>--请选择--</option>");
    // 遍历赋值市级下拉列表
    $.each(areaJson[prov_id].children, function (i, city) {
        $("#cityCode").append("<option value='" + city.value + "'>" + city.text + "</option>");
    });
        layui.form.render('select');
}

// 赋值地区（县）函数
function distSelect(city_id) {
    if (prov_id < 0 || city_id < 0
        || typeof (areaJson[prov_id].children[city_id].children) == "undefined") {
        return;
    }
    $("#districtCode").append("<option value=''>--请选择--</option>");
    $.each(areaJson[prov_id].children[city_id].children, function (i, dist) {
        $("#districtCode").append("<option value='" + dist.value + "'>" + dist.text + "</option>");
    });
    layui.form.render('select');
}

// 赋值地区（县）函数
function distSelectFirst(p_id, city_id) {
    if (p_id < 0 || city_id < 0
        || typeof (areaJson[p_id].children[city_id].children) == "undefined") {
        return;
    }
    $("#districtCode").append("<option value=''>--请选择--</option>");
    $.each(areaJson[p_id].children[city_id].children, function (i, dist) {
        $("#districtCode").append("<option value='" + dist.value + "'>" + dist.text + "</option>");
    });

}

layui.form.on('select(province)', function (data) {
    $("#cityCode").html("<option value=''>--请选择--</option>");
    prov_id = data.elem.selectedIndex - 1;
    citySelect(prov_id);
});

layui.form.on('select(city)', function (data) {
    $("#districtCode").html("<option value=''>--请选择--</option>");
    city_id = data.elem.selectedIndex - 2;
    distSelect(city_id);
});
var prov_id = -1;

function setup() {
    provSelect();
}